/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package Servlets;

import Beans.Ciudad;
import Beans.Hotel;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Blob;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.UnavailableException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Micaela
 */
public class BajaModHabitacion extends HttpServlet {

     private Connection con;
     @Override
     public void init() throws ServletException{
        try{
            Class.forName("com.mysql.jdbc.Driver").newInstance();
            con = DriverManager.getConnection("jdbc:mysql://localhost/Agencia","root","root");
        }
        catch(Exception ex){
            throw new UnavailableException("Error al intentar conectar con la bd :(: "+ex.getMessage());
        }
    }
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
       if(request.getParameter("baja")!=null){
                              
                try {
                     int numero=  Integer.parseInt(request.getParameter("Numero"));
                     int idhotel= Integer.parseInt(request.getParameter("IdHotel"));
                     CallableStatement cs= con.prepareCall("{call BajaHabitacion(?,?)}");
                     cs.setInt(1, numero);
                     cs.setInt(2,idhotel);                     
                     cs.executeUpdate();
                     response.sendRedirect("MenuPrincipal.jsp");
                    }
                catch(SQLException ex){
                throw new UnavailableException("Error en la bd :( :" + ex.getMessage());
                  }             
          }
        else{
            if((request.getParameter("Tipo")!= null)&&(request.getParameter("Precio")!=null)
                    &&(request.getParameter("Capacidad")!= null)){//solo podra cambiar esos atributos, porque de cambiar algo del hotel o el identificador de habitación estaría creando uno nuev
               try{
                    int num=Integer.parseInt(request.getParameter("Numero"));
                    int idhotel=Integer.parseInt(request.getParameter("IdHotel"));
                    double precio=Double.parseDouble(request.getParameter("Precio"));
                    String tipo =request.getParameter("Tipo");
                    int capacidad =Integer.parseInt(request.getParameter("Capacidad"));
                    CallableStatement cs=con.prepareCall("{call ModificarHabitacion(?,?,?,?,?)}");
                    cs.setInt(1, num);
                    cs.setInt(2, idhotel);
                    cs.setString(3, tipo);
                    cs.setInt(4, capacidad);
                    cs.setDouble(5, precio);
                    cs.executeUpdate();
                    response.sendRedirect("MenuPrincipal.jsp");
                }
                catch(IOException ex){
                    throw new UnavailableException("Error unaviEx IOEx :( "+ex.getMessage());
                }
                catch(NumberFormatException ex){
                    throw new UnavailableException("ingrese numeros por favor -.- ");
                }
                catch(SQLException ex){
                    throw new UnavailableException("Error en bd"+ex.getMessage());
                }
                
            }
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

    
    }




